<?php

namespace app\stock\controller\business;

use think\Controller;
// use app\common\model\Business;

//回收站管理
class Recyclebin extends Controller
{
  public function _initialize()
  {
    parent::_initialize();
    $this->BusinessModel = model('Business.Business');
    $this->AdminModel = model('Admin');
  }

  // 列表
  public function index()
  {
    $adminid = $this->request->param('adminid', 0, 'trim'); //管理员id
    $admininfo = $this->AdminModel->find($adminid);
    if (!$admininfo) {
      $this->error('管理员不存在');
    }
    //获取软删除数据
    // $data =$this->BusinessModel->withTrashed()->whereNotNull('deletetime')->with('source')->select();
    $data =  $this->BusinessModel->onlyTrashed()
      ->with('source')
      ->select();
    //判断！！！
    $this->success('', '', $data);
  }

  //还原到公海
  public function revert()
  {
    $adminid = $this->request->param('adminid', 0, 'trim'); //管理员id
    $busid = $this->request->param('busid', 0, 'trim');
    $admininfo = $this->AdminModel->find($adminid);
    if (!$admininfo) {
      $this->error('管理员不存在');
    }
    // $this->BusinessModel->onlyTrashed()->restore();//->fetchSql()
    // $result =  $this->BusinessModel->onlyTrashed()->where('id', $busid)->restore();
    $result = $this->BusinessModel->onlyTrashed()->where('id', $busid)->update(['deletetime' => null]);

    $this->success('', '', $result);
  }

  //删除（真删）
  public function del()
  {
    $adminid = $this->request->param('adminid', 0, 'trim'); //管理员id
    $busid = $this->request->param('busid', 0, 'trim');
    $admininfo = $this->AdminModel->find($adminid);
    if (!$admininfo) {
      $this->error('管理员不存在');
    }
    
    //真删
    $result = $this->BusinessModel->onlyTrashed()->whereNotNull('deletetime')->where('id', $busid)->delete(true);
    $this->success('', '', $result);
  }
}
